define(function(require,exports,module){
	var Subnav = require("subnav");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var user = require("store").get("user");
	
	module.exports = {
		initComponent: function(params,widget){
			var subnav = new Subnav({
				parentNode: ".J-subnav",
				model: {
					title: "餐饮落实",
					items: [{
						id:"search",
						type: "search",
						placeholder: "组团单位",
						handler: function(str){
							aw.ajax({
								url: "api/checkincar/search",
								data: {
									s: str,
									properties: "checkInImplement.groupReserve.planItemGroup.servicePoint.name," +
										"checkInImplement.groupReserve.planItemGroup.allianceOrg.name," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.groupUnit",
									fetchProperties: "*,checkInImplement.version," +
										"checkInImplement.pkCIImplement," +
										"checkInImplement.signUpNum," +
										"checkInImplement.dataSource.*," +
										"status.*," +
										"checkInImplement.groupReserve.*," +
										"checkInImplement.groupReserve.planItemGroup.allianceOrg.*," +
										"checkInImplement.groupReserve.planItemGroup.planItem.*," +
										"checkInImplement.groupReserve.planItemGroup.planItem.destination.*," +
										"checkInImplement.groupReserve.planItemGroup.servicePoint.pkServicePoint," +
										"checkInImplement.groupReserve.planItemGroup.servicePoint.name," +
										"checkInImplement.groupReserve.planItemGroup.signUps.version," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.*," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.groupUnit," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUpType.*," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," +
										"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUps.version" 
								},
								success: function(data){
									widget.get("grid").setData(data);
								}
							});
						}
					},{
						id : "servicePoint",
						text : "服务点",
						tip : "服务点",
//						url:"api/servicepoint/operableservicepoints",
//						lazy:true,
						items:user.privateServicePoints,
						type : "buttongroup",
						keyField : "pkServicePoint",
						valueField : "name",
						all: {
							show: true,
							position: "bottom"
						},
						handler:function(key,element){	
							widget.get("grid").refresh();
						}
					},{
						id:"checkStatus",
						type: "buttongroup",
						tip: "落实状态",
						items: [{
							key: "Pending",
							value: "待落实"
						},{
							key: "Pended",
							value: "已落实"
						}],
						all: {
							show: true,
							position: "bottom"
						},
						handler: function(key){
							widget.get("grid").refresh();
						}
					},{
			            id: "dateRange",
			            tip: "入住日期",
			            type: "daterange",
			            ranges: {
			                "今天": [moment().startOf("days"), moment().endOf("days")],
			                "本月": [moment().startOf("month"), moment().endOf("month")]
			            },
			            defaultRange: "本月",
			            minDate: "1930-05-31",
			            maxDate: "2020-12-31",
			            handler: function(time) {
			            	widget.get("grid").refresh();
			            }
			        },{
			        	id:"save",
			        	type:"button",
			        	text:"保存",
			        	show:false,
			        	handler:function(){
			        		var form = widget.get("form");
			        		var data = form.getData();
			       			
			        		var breakfastCheck = false;
			        		var lunchCheck = false;
			        		var supperCheck = false;
			        		
			        		if(data.haveDinner=="true" && data.haveDinner != null && data.haveDinner != ""){
			        			if(data.breakfast == "false" || data.breakfast == null || data.breakfast == ""){
			        				data.breakfastConfirm = "false";
			        				breakfastCheck = true;
			        			}else{
			        				$(".J-grid-checkbox:checked").parent().next().each(function(){
			        					if($(this).children().html()=="早餐"){
			        						data.breakfastConfirm = "true";
			        						breakfastCheck = true;
			        					}
			        				})
			        			}
			        			if(data.lunch == "false" || data.lunch == null || data.lunch == ""){
			        				data.lunchConfirm = "false";
			        				lunchCheck = true;
			        			}else{
			        				$(".J-grid-checkbox:checked").parent().next().each(function(){
			        					if($(this).children().html()=="午餐"){
			        						data.lunchConfirm = "true";
			        						lunchCheck = true;
			        					}
			        				})
			        			}
			        			if(data.supper == "false" || data.supper == null || data.supper == ""){
			        				data.supperConfirm = "false";
			        				supperCheck = true;
			        			}else{
			        				$(".J-grid-checkbox:checked").parent().next().each(function(){
			        					if($(this).children().html()=="晚餐"){
			        						data.supperConfirm = "true";
			        						supperCheck = true;
			        					}
			        				})
			        			}
			        		}
			        		
			        		if(data.haveDinner=="true"){
			       				if(breakfastCheck && lunchCheck && supperCheck ){
			       					data.status="Pended"
			       				}else{
			       					Dialog.alert({
			       						content:"您存在未落实项！"
			       					});
			       					return;
			       				}
			       				
			       			}
			        		
			       			aw.saveOrUpdate("api/checkindinner/confirm",data,function(data){
			       				widget.show([".J-grid"]).hide([".J-form",".J-gridEnd"]);
								widget.get("subnav").hide(["back","save"]).show(["search","checkStatus","servicePoint","dateRange"]);
								widget.get("grid").refresh();
			       			});
			        	}
			        },{
						id:"back",
						type:"button",
						text:"返回",
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-form",".J-gridEnd"]);
							widget.get("subnav").hide(["back","save"]).show(["search","checkStatus","servicePoint","dateRange"]);
							widget.get("grid").refresh();
						}
					}]
				}
			});
			
			widget.set("subnav",subnav);
		}
	};
});